
<template>
	<view>
		<navbar title="绑定用户名">
		</navbar>
		<view style="padding: 0 20px;background-color: white;">
			<u--form :model="registerdata" :rules="rules" ref="rForm">
				<u-form-item prop="username">
					<u--input v-model="registerdata.username" shape="circle" placeholder="请输入用户名" prefixIcon="account"
						prefixIconStyle="font-size: 22px;color: #909399"></u--input>
				</u-form-item>
				<u-form-item prop="password">
					<u--input v-model="registerdata.password" shape="circle" placeholder="请输入密码" prefixIcon="lock"
						prefixIconStyle="font-size: 22px;color: #909399"></u--input>
				</u-form-item>
				<u-form-item prop="password">
					<u--input v-model="password" shape="circle" placeholder="请再次输入密码" prefixIcon="lock-open"
						prefixIconStyle="font-size: 22px;color: #909399"></u--input>
				</u-form-item>
				<uni-captcha focus ref="captcha" scene="register" style="margin-top: 5px;" v-model="registerdata.captcha" />
				<u-button @click="ureg" text="注册" style="margin-top: 15px;" type="primary" shape="circle"></u-button>
			</u--form>
		</view>
	</view>
</template>

<script>
	const uniIdCo = uniCloud.importObject("uni-id-co")
	export default {
		methods:{
			remove() {
				uniIdCo.logout().then(e=>{
					uni.removeStorageSync('uni_id_token')
					uni.removeStorageSync('uni_id_token_expired')
					uni.reLaunch({
						url: '../index/index'
					});
				})
			},
			async getuser_id() {
				const userid = await uniCloud.callFunction({
					name: 'getuser_id'
				})
				this.user_id = userid.result.data
			},
			ureg() {
				var that=this
				if (this.password != this.registerdata.password) {
					this.messae('密码不一致','error')
					return false
				}
				this.$refs.rForm.validate().then((res) => {
					uniIdCo.registerUser(this.registerdata).then(e => {
							uniCloud.callFunction({
								name: 'setusername',
								data:{
									id:e.uid,
									user_id:this.user_id
								},
								success(res) {
									if(res.result.code==200){
										that.message("绑定成功")
										that.remove()
										uni.reLaunch({
											url: '../login/login'
										});
									}else{
										that.message('绑定失败','error')
									}
								}
							});
							
						})
						.catch(e => {
							console.log(e.message);
						})
				}).catch((errors) => {
					that.message('注册失败','error')
				})
			}
		},
		onReady() {
			this.getuser_id()
		},
		data() {
			return {
				user_id:null,
				needCaptcha:false,
				password: null,
				registerdata: {
					code: "",
					nickname: "临时昵称",
					password: null,
					captcha: null
				},
				rules: {
					'nickname': [{
						type: 'string',
						required: true,
						message: '请填写用户昵称'
					},{
						type: 'string',
						min: 2,
						max: 8,
						message: '用户昵称长度应为2-8位'
					}],
					'username':[{
						type: 'string',
						pattern: /^[a-zA-Z]+[0-9]*$/g,
						message: '用户名为字母或数字组合'
					},{
						type: 'regexp',
						required: true,
						message: '请填写用户名'
					},{
						type: 'string',
						message: '用户名长度应该为4-12位',
						min: 4,
						max: 12
					}],
					'password':[{
						type: 'string',
						message: '密码长度应该为2-16位',
						min: 4,
						max: 16
					},{
						type: 'string',
						required: true,
						message: '请填写用户密码'
					}]
				}
			};
		}
	}
</script>

<style lang="less">
page{
	background-color: white;
}
</style>
